package com.example.myblog.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myblog.entity.BlogMessage;
import com.example.myblog.exception.BaseException;
import com.example.myblog.result.ResponseBean;
import com.example.myblog.service.BlogMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客基本信息 前端控制器
 * </p>
 *
 * @author 叶兴
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/blog-message")
public class BlogMessageController {

    BlogMessageService blogMessageService;

    @Autowired
    public void setBlogMessageService(BlogMessageService blogMessageService) {
        this.blogMessageService = blogMessageService;
    }

    /**
     * 获取当前用户的所有博客信息
     * @return
     * @throws BaseException
     */
    @GetMapping("/user-blogs")
    public ResponseBean getUserBlogs() throws BaseException {
        List<BlogMessage> list = blogMessageService.getAllBlogs();
        return ResponseBean.success(list);
    }

    @GetMapping("/blogs")
    public ResponseBean getAllBlogs(@RequestParam(defaultValue = "1")Integer pageNum) throws BaseException {
        Map<String, Object> map = blogMessageService.getBlogs(pageNum);
        return ResponseBean.success(map);
    }

    @GetMapping("/category")
    public ResponseBean getCategoryBlogs(@RequestParam(defaultValue = "1")Integer pageNum,
                                         @RequestParam("categoryName") String categoryName) throws BaseException {
        Map<String, Object> map = blogMessageService.getCategoryBlogs(pageNum,categoryName);
        return ResponseBean.success(map);
    }
}
